﻿using CIS.DB;
using CIS.Web.Common;
using CIS.Web.Models;
using CIS.Web.Properties;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Imaging;

namespace CIS.Web.Controllers
{
	[RoutePrefix("MeetingAttendence")]
	public class MeetingAttendenceController : BaseDbController
	{
		public async Task<ActionResult> Index(string conferenceId)
		{
			var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == conferenceId && x.Status == ConstValues.Status.正常);

			if (conferenceMaster == null)
			{
				return RedirectToAction(nameof(HomeController.MainIndex), nameof(HomeController).GetControllerName());
			}

			var model = new MeetingFormModel();
			model.ConferenceId = conferenceId;
			model.Introduction = conferenceMaster.Introduction == null ? string.Empty : conferenceMaster.Introduction;
			model.AttendenceNotice = conferenceMaster.AttendenceNotice == null ? string.Empty : conferenceMaster.AttendenceNotice.Replace("\r\n", string.Empty);
			model.Language = conferenceMaster.IsInternational ? ConstValues.Language.En : ConstValues.Language.Cn;

			var allowRegister = conferenceMaster.RegisterStartDate.Value <= DateTime.Now && conferenceMaster.RegisterEndDate.Value >= DateTime.Now ? true : false;
			if (allowRegister == false)
			{
				return View("RegisterClose", this.GetLoginModel());
			}

			var conferenceAttendenceForm = await DbContext.ConferenceAttendenceForm.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 &&
						x.ConferenceId == conferenceId)
					.ToListAsync();
			model.TotalPage = conferenceAttendenceForm.Max(x => x.DisplayPage).Value;

			var concurrentEvent = DbContext.ConferenceAttendenceForm.AsNoTracking()
					.FirstOrDefault(x => x.Status == ConstValues.Status.正常 &&
						x.ConferenceId == conferenceId &&
						x.Key == ConstValues.FunctionNameConcurrentEvent);
			model.ConcurrentEventPage = concurrentEvent == null ? string.Empty : concurrentEvent.DisplayPage.ToString();

			model.CanRecommend = DbContext.ConferenceAttendenceForm.AsNoTracking()
					.Any(x => x.Status == ConstValues.Status.正常 &&
						x.ConferenceId == conferenceId &&
						x.Key == "Recommend");

			Session.Remove("TmpCode");

			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", conferenceId);

			ViewBag.ConferenceId = conferenceId;
			ViewBag.ConferenceName = conferenceMaster.ConferenceName;

			return View(model);
		}

		[Route("GetCurrentPageItem/{conferenceId}/{page:int}")]
		public async Task<JsonCamelCaseResult> GetCurrentPageItem(string conferenceId, int page)
		{
			var result = await DbContext.ConferenceAttendenceForm.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 &&
						x.ConferenceId == conferenceId &&
						x.DisplayPage == page)
					.OrderBy(x => x.Sort)
					.ThenBy(x => x.DisplayName)
					.ToListAsync();

			return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty, result),
					JsonRequestBehavior.AllowGet);
		}

		[Route("GetCurrentPageValue/{conferenceId}")]
		public async Task<JsonCamelCaseResult> GetCurrentPageValue(string conferenceId)
		{
			var code = Session["TmpCode"]?.ToString();

			var sqlResult = await DbContext.ConferenceAttendenceInfoTmp.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == code);

			if (sqlResult == null)
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(),
						JsonRequestBehavior.AllowGet);
			}

			var result = new
			{
				ConferenceId = sqlResult.ConferenceId,
				Mail = sqlResult.Mail,
				FamilyName = sqlResult.FamilyName,
				FirstName = sqlResult.FirstName,
				FullName = sqlResult.FullName,
				Gender = sqlResult.Gender,
				Title = sqlResult.Title,
				Degree = sqlResult.Degree,
				Job = sqlResult.Job,
				Affiliation = sqlResult.Affiliation,
				Department = sqlResult.Department,
				Country = sqlResult.Country,
				State = sqlResult.StateStr,
				City = sqlResult.CityStr,
				Address = sqlResult.Address,
				ZipCode = sqlResult.ZipCode,
				Telephone = sqlResult.Telephone,
				Mobile = sqlResult.Mobile,
				CardId = sqlResult.CardId,
				AttendenceIdentityId = sqlResult.AttendenceIdentityId,
				AttendenceFeeId = sqlResult.AttendenceFeeId,
				PageFeeId = sqlResult.PageFeeId,
				MealFeeId = sqlResult.MealFeeId,
				OtherFeeList = sqlResult.OtherFeeList,
				OtherFeeCount = sqlResult.OtherFeeCount,
				OtherFeeName = Utility.GetNameFromIdList(sqlResult.OtherFeeList, "otherFee"),
				InvoiceTitle = sqlResult.InvoiceTitle,
				TaxNum = sqlResult.TaxNum,
				AddressAndTel = sqlResult.AddressAndTel,
				Bank = sqlResult.Bank,
				BankAccount = sqlResult.BankAccount,
				InvoiceId = sqlResult.InvoiceId,
				FavouriteTopic = sqlResult.FavouriteTopicId.Split(ConstValues.chrSemicolon),
				Customize1 = sqlResult.Customize1,
				Customize2 = sqlResult.Customize2,
				Customize3 = sqlResult.Customize3,
				Customize4 = sqlResult.Customize4,
				Customize5 = sqlResult.Customize5,
				Customize6 = sqlResult.Customize6,
				Customize7 = sqlResult.Customize7,
				Customize8 = sqlResult.Customize8,
				Customize9 = sqlResult.Customize9,
				Customize10 = sqlResult.Customize10,
				ConcurrentEventList = sqlResult.ConcurrentEventList,
				ConcurrentEventCount = sqlResult.ConcurrentEventCount,
				PaymentTypeId = sqlResult.PaymentTypeId,
			};

			return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty, result),
					JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		public async Task<JsonResult> CheckMail(ConferenceAttendenceInfoModel model)
		{
			if (!string.IsNullOrEmpty(model.SelfMail))
			{
				var result = await DbContext.ConferenceAttendenceInfo.AsNoTracking()
						.FirstOrDefaultAsync(x => x.ConferenceId == model.ConferenceId &&
							x.Mail == LoginUser.Email);

				if (result != null)
				{
					return JsonCamelCase(Utility.CreateFailJsonResult(), JsonRequestBehavior.AllowGet);
				}
			}
			else
			{
				var result = await DbContext.ConferenceAttendenceInfo.AsNoTracking()
						.FirstOrDefaultAsync(x => x.ConferenceId == model.ConferenceId &&
							x.Mail == model.Mail);

				if (result != null)
				{
					return JsonCamelCase(Utility.CreateFailJsonResult(), JsonRequestBehavior.AllowGet);
				}
			}

			return JsonCamelCase(Utility.CreateSuccessJsonResult(), JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		public async Task<JsonResult> GetUserInfoByMail(ConferenceAttendenceInfoModel model)
		{
			var code = Session["TmpCode"]?.ToString();

			if (!string.IsNullOrEmpty(code))
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(),
						JsonRequestBehavior.AllowGet);
			}

			var conferenceInfo = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == model.ConferenceId);

			var infoEn = await DbContext.SysUserMasterEn.AsNoTracking()
					.FirstOrDefaultAsync(x => x.Mail == LoginUser.Email);

			var info = await DbContext.SysUserMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.Mail == LoginUser.Email);

			code = Utility.GetTableId(ConstValues.TableName.ConferenceAttendenceInfo);
			var tmp = new ConferenceAttendenceInfoTmp
			{
				ID = code,
				ConferenceId = model.ConferenceId,
				Mail = LoginUser.Email,
				EssayId = string.Empty,
				FavouriteTopicId = string.Empty,
				HasVisa = false,
				HasHotel = false,
				HasMeal = false,
				HasPage = false,
				TotalFee = 0,
				Discount = 0,
				Currency = !string.IsNullOrEmpty(model.Currency) ? model.Currency : ConstValues.Currency.人民币,
				InvoiceTitle = info.InvoiceTitle,
				TaxNum = info.TaxNum,
				AddressAndTel = info.AddressAndTel,
				Bank = info.Bank,
				BankAccount = info.BankAccount,
				Sort = ConstValues.DefaultSort,
				Creator = LoginUser.UserName,
				CreateIP = LoginUser.IP,
				CreateDate = DateTime.Now
			};
			DbContext.ConferenceAttendenceInfoTmp.Add(tmp);

			if (conferenceInfo.IsInternational && infoEn != null && infoEn.HasUpdate)
			{
				tmp.FamilyName = infoEn.FamilyName;
				tmp.FirstName = infoEn.FirstName;
				tmp.FullName = infoEn.FullName;
				tmp.Gender = infoEn.Gender;
				tmp.Degree = infoEn.Degree;
				tmp.Country = infoEn.Country;
				tmp.StateStr = infoEn.StateStr;
				tmp.CityStr = infoEn.CityStr;
				tmp.Address = infoEn.Address;
				tmp.Telephone = infoEn.Telephone;
				tmp.Mobile = infoEn.Mobile;
				tmp.Affiliation = infoEn.Affiliation;
				tmp.Department = infoEn.Department;
				tmp.Job = Utility.GetNameFromIdList(info.Career, "career", ConstValues.Language.En);
			}
			else if (!conferenceInfo.IsInternational && info.HasUpdate)
			{
				tmp.FamilyName = info.FamilyName;
				tmp.FirstName = info.FirstName;
				tmp.FullName = info.FullName;
				tmp.Gender = info.Gender;
				tmp.Degree = info.Degree;
				tmp.Country = info.Country;
				tmp.StateStr = info.StateStr;
				tmp.CityStr = info.CityStr;
				tmp.Address = info.Address;
				tmp.ZipCode = info.ZipCode;
				tmp.Telephone = info.Telephone;
				tmp.Mobile = info.Mobile;
				tmp.Affiliation = info.Affiliation;
				tmp.Department = info.Department;
				tmp.Job = Utility.GetNameFromIdList(info.Career, "career", ConstValues.Language.Cn);
			}

			Session["TmpCode"] = code;
			await DbContext.SaveChangesAsync();

			return JsonCamelCase(Utility.CreateSuccessJsonResult(),
					JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public async Task<JsonResult> CreateTmp(string isSelf, string currentPage, ConferenceAttendenceInfoModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId);

			var code = Session["TmpCode"]?.ToString();

			if (string.IsNullOrEmpty(code))
			{
				code = Utility.GetTableId(ConstValues.TableName.ConferenceAttendenceInfo);
				var conferenceAttendenceInfo = new ConferenceAttendenceInfoTmp
				{
					ID = code,
					ConferenceId = model.ConferenceId,
					Mail = model.Mail?.Trim(),
					FamilyName = model.FamilyName?.Trim(),
					FirstName = model.FirstName?.Trim(),
					FullName = model.FullName?.Trim(),
					Gender = true,
					Title = model.Title,
					Degree = model.Degree,
					Job = model.Job?.Trim(),
					Affiliation = model.Affiliation?.Trim(),
					Department = model.Department?.Trim(),
					Country = model.Country,
					StateStr = model.State,
					CityStr = model.City,
					Address = model.Address?.Trim(),
					ZipCode = model.ZipCode?.Trim(),
					Telephone = model.Telephone?.Trim(),
					Mobile = model.Mobile?.Trim(),
					CardId = model.CardId?.Trim(),
					AttendenceIdentityId = model.AttendenceIdentityId,
					PageFeeId = !string.IsNullOrEmpty(model.PageFeeId) ? model.PageFeeId : string.Empty,
					AttendenceFeeId = !string.IsNullOrEmpty(model.AttendenceFeeId) ? model.AttendenceFeeId : string.Empty,
					MealFeeId = !string.IsNullOrEmpty(model.MealFeeId) ? model.MealFeeId : string.Empty,
					OtherFeeList = model.OtherFee,
					OtherFeeCount = model.OtherFeeCount,
					InvoiceTitle = model.InvoiceTitle?.Trim(),
					TaxNum = model.TaxNum?.Trim(),
					AddressAndTel = model.AddressAndTel?.Trim(),
					Bank = model.Bank?.Trim(),
					BankAccount = model.BankAccount?.Trim(),
					InvoiceId = model.InvoiceId,
					FavouriteTopicId = model.FavouriteTopic != null ? string.Join(ConstValues.strSemicolon, model.FavouriteTopic) : string.Empty,
					Customize1 = model.Customize1?.Trim(),
					Customize2 = model.Customize2?.Trim(),
					Customize3 = model.Customize3?.Trim(),
					Customize4 = model.Customize4?.Trim(),
					Customize5 = model.Customize5?.Trim(),
					Customize6 = model.Customize6?.Trim(),
					Customize7 = model.Customize7?.Trim(),
					Customize8 = model.Customize8?.Trim(),
					Customize9 = model.Customize9?.Trim(),
					Customize10 = model.Customize10?.Trim(),
					PaymentTypeId = model.PaymentTypeId,
					HasVisa = false,
					HasHotel = false,
					HasMeal = false,
					HasPage = false,
					TotalFee = 0,
					Discount = 0,
					Currency = !string.IsNullOrEmpty(model.Currency) ? model.Currency : ConstValues.Currency.人民币,
					Sort = ConstValues.DefaultSort,
					Creator = LoginUser.UserName,
					CreateIP = LoginUser.IP,
					CreateDate = DateTime.Now
				};

				if (isSelf == ConstValues.GenericSelectionType.有)
				{
					var sysUserMaster = await DbContext.SysUserMaster.FirstOrDefaultAsync(x => x.Mail == LoginUser.Email);
					if (sysUserMaster != null)
					{
						conferenceAttendenceInfo.InvoiceTitle = sysUserMaster.InvoiceTitle;
						conferenceAttendenceInfo.TaxNum = sysUserMaster.TaxNum;
						conferenceAttendenceInfo.AddressAndTel = sysUserMaster.AddressAndTel;
						conferenceAttendenceInfo.Bank = sysUserMaster.Bank;
						conferenceAttendenceInfo.BankAccount = sysUserMaster.BankAccount;
					}
				}

				DbContext.ConferenceAttendenceInfoTmp.Add(conferenceAttendenceInfo);

				Session["TmpCode"] = code;
			}
			else
			{
				var result = await DbContext.ConferenceAttendenceInfoTmp
						.FirstOrDefaultAsync(x => x.ID == code);

				if (!(string.IsNullOrEmpty(model.Mail)) && result.Mail != model.Mail)
				{
					result.Mail = model.Mail.Trim();
				}
				if (!(string.IsNullOrEmpty(model.FamilyName)) && result.FamilyName != model.FamilyName)
				{
					result.FamilyName = model.FamilyName.Trim();
				}
				if (!(string.IsNullOrEmpty(model.FirstName)) && result.FirstName != model.FirstName)
				{
					result.FirstName = model.FirstName.Trim();
				}
				if (!(string.IsNullOrEmpty(model.FullName)) && result.FullName != model.FullName)
				{
					result.FullName = model.FullName.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Gender)))
				{
					result.Gender = model.Gender == ConstValues.Gender.男 ? true : false;
				}
				if (!(string.IsNullOrEmpty(model.Title)) && result.Title != model.Title)
				{
					result.Title = model.Title;
				}
				if (model.Degree.HasValue && result.Degree != model.Degree.Value)
				{
					result.Degree = model.Degree.Value;
				}
				if (!(string.IsNullOrEmpty(model.Job)) && result.Job != model.Job)
				{
					result.Job = model.Job.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Affiliation)) && result.Affiliation != model.Affiliation)
				{
					result.Affiliation = model.Affiliation.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Department)) && result.Department != model.Department)
				{
					result.Department = model.Department.Trim();
				}
				if (model.Country.HasValue && result.Country != model.Country.Value)
				{
					result.Country = model.Country.Value;
				}
				if (!string.IsNullOrEmpty(model.State) && result.StateStr != model.State)
				{
					result.StateStr = model.State;
				}
				if (!string.IsNullOrEmpty(model.City) && result.CityStr != model.City)
				{
					result.CityStr = model.City;
				}
				if (!(string.IsNullOrEmpty(model.Address)) && result.Address != model.Address)
				{
					result.Address = model.Address.Trim();
				}
				if (!(string.IsNullOrEmpty(model.ZipCode)) && result.ZipCode != model.ZipCode)
				{
					result.ZipCode = model.ZipCode.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Telephone)) && result.Telephone != model.Telephone)
				{
					result.Telephone = model.Telephone.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Mobile)) && result.Mobile != model.Mobile)
				{
					result.Mobile = model.Mobile.Trim();
				}
				if (!(string.IsNullOrEmpty(model.CardId)) && result.CardId != model.CardId)
				{
					result.CardId = model.CardId.Trim();
				}
				if (!(string.IsNullOrEmpty(model.AttendenceIdentityId)) && result.AttendenceIdentityId != model.AttendenceIdentityId)
				{
					result.AttendenceIdentityId = model.AttendenceIdentityId;
				}
				if (!string.IsNullOrEmpty(model.PageFeeId) && result.PageFeeId != model.PageFeeId)
				{
					result.PageFeeId = model.PageFeeId;
				}
				if (!string.IsNullOrEmpty(model.AttendenceFeeId) && result.AttendenceFeeId != model.AttendenceFeeId)
				{
					result.AttendenceFeeId = model.AttendenceFeeId;
				}
				if (!string.IsNullOrEmpty(model.MealFeeId) && result.MealFeeId != model.MealFeeId)
				{
					result.MealFeeId = model.MealFeeId;
				}
				if (!string.IsNullOrEmpty(model.OtherFee))
				{
					result.OtherFeeList = model.OtherFee;
				}
				if (!string.IsNullOrEmpty(model.OtherFeeCount))
				{
					result.OtherFeeCount = model.OtherFeeCount;
				}
				if (!(string.IsNullOrEmpty(model.InvoiceTitle)) && result.InvoiceTitle != model.InvoiceTitle)
				{
					result.InvoiceTitle = model.InvoiceTitle.Trim();
				}
				if (!(string.IsNullOrEmpty(model.TaxNum)) && result.TaxNum != model.TaxNum)
				{
					result.TaxNum = model.TaxNum.Trim();
				}
				if (!(string.IsNullOrEmpty(model.AddressAndTel)) && result.AddressAndTel != model.AddressAndTel)
				{
					result.AddressAndTel = model.AddressAndTel.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Bank)) && result.Bank != model.Bank)
				{
					result.Bank = model.Bank.Trim();
				}
				if (!(string.IsNullOrEmpty(model.BankAccount)) && result.BankAccount != model.BankAccount)
				{
					result.BankAccount = model.BankAccount.Trim();
				}
				if (!string.IsNullOrEmpty(model.InvoiceId))
				{
					result.InvoiceId = model.InvoiceId;
				}
				if (model.FavouriteTopic != null && model.FavouriteTopic.Length > 0)
				{
					result.FavouriteTopicId = string.Join(ConstValues.strSemicolon, model.FavouriteTopic);
				}
				if (!(string.IsNullOrEmpty(model.Customize1)) && result.Customize1 != model.Customize1)
				{
					result.Customize1 = model.Customize1.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize2)) && result.Customize2 != model.Customize2)
				{
					result.Customize2 = model.Customize2.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize3)) && result.Customize3 != model.Customize3)
				{
					result.Customize3 = model.Customize3.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize4)) && result.Customize4 != model.Customize4)
				{
					result.Customize4 = model.Customize4.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize5)) && result.Customize5 != model.Customize5)
				{
					result.Customize5 = model.Customize5.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize6)) && result.Customize6 != model.Customize6)
				{
					result.Customize6 = model.Customize6.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize7)) && result.Customize7 != model.Customize7)
				{
					result.Customize7 = model.Customize7.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize8)) && result.Customize8 != model.Customize8)
				{
					result.Customize8 = model.Customize8.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize9)) && result.Customize9 != model.Customize9)
				{
					result.Customize9 = model.Customize9.Trim();
				}
				if (!(string.IsNullOrEmpty(model.Customize10)) && result.Customize10 != model.Customize10)
				{
					result.Customize10 = model.Customize10.Trim();
				}
				if (!string.IsNullOrEmpty(model.PaymentTypeId))
				{
					result.PaymentTypeId = model.PaymentTypeId;
				}

				var attendForm = await DbContext.ConferenceAttendenceForm.FirstOrDefaultAsync(x => x.ConferenceId == model.ConferenceId
							&& x.Key == "ConcurrentEvent" && x.Status == ConstValues.Status.正常);
				if (attendForm != null)
				{
					if (currentPage == attendForm.DisplayPage.Value.ToString())
					{
						result.ConcurrentEventList = model.HidEventIdList;
						result.ConcurrentEventCount = model.HidCountList;
					}
				}
			}

			await DbContext.SaveChangesAsync();

			return JsonCamelCase(Utility.CreateSuccessJsonResult(),
					JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public async Task<JsonResult> Submit(string isSelf, string payType, VisaModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId);

			var code = Session["TmpCode"]?.ToString();

			var infoTmp = await DbContext.ConferenceAttendenceInfoTmp
					.FirstOrDefaultAsync(x => x.ID == code);

			if (infoTmp != null && string.IsNullOrEmpty(infoTmp.Mail))
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgRegistrationMust, code),
						JsonRequestBehavior.AllowGet);
			}

			using (var transaction = DbContext.Database.BeginTransaction())
			{
				#region ConferenceAttendenceInfo
				var result = new ConferenceAttendenceInfo
				{
					ID = code,
					ConferenceId = infoTmp.ConferenceId,
					Mail = infoTmp.Mail?.Trim(),
					FamilyName = infoTmp.FamilyName?.Trim(),
					FirstName = infoTmp.FirstName?.Trim(),
					FullName = string.IsNullOrEmpty(infoTmp.FullName?.Trim()) ? Utility.SetFullName(infoTmp.FamilyName?.Trim(), infoTmp.FirstName?.Trim(),
								Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En ?
									ConstValues.Language.En : ConstValues.Language.Cn) : infoTmp.FullName?.Trim(),
					Gender = infoTmp.Gender,
					Title = infoTmp.Title?.Trim(),
					Degree = infoTmp.Degree != 0 ? infoTmp.Degree : null,
					Job = infoTmp.Job?.Trim(),
					Affiliation = infoTmp.Affiliation?.Trim(),
					Department = infoTmp.Department?.Trim(),
					Country = infoTmp.Country,
					StateStr = infoTmp.StateStr,
					CityStr = infoTmp.CityStr,
					Address = infoTmp.Address?.Trim(),
					ZipCode = infoTmp.ZipCode?.Trim(),
					Telephone = infoTmp.Telephone?.Trim(),
					Mobile = infoTmp.Mobile?.Trim(),
					CardId = infoTmp.CardId?.Trim(),
					AttendenceIdentityId = infoTmp.AttendenceIdentityId,
					InvoiceTitle = infoTmp.InvoiceTitle?.Trim(),
					TaxNum = infoTmp.TaxNum?.Trim(),
					AddressAndTel = infoTmp.AddressAndTel?.Trim(),
					Bank = infoTmp.Bank?.Trim(),
					BankAccount = infoTmp.BankAccount?.Trim(),
					InvoiceId = infoTmp.InvoiceId,
					FavouriteTopicId = infoTmp.FavouriteTopicId,
					Customize1 = infoTmp.Customize1?.Trim(),
					Customize2 = infoTmp.Customize2?.Trim(),
					Customize3 = infoTmp.Customize3?.Trim(),
					Customize4 = infoTmp.Customize4?.Trim(),
					Customize5 = infoTmp.Customize5?.Trim(),
					Customize6 = infoTmp.Customize6?.Trim(),
					Customize7 = infoTmp.Customize7?.Trim(),
					Customize8 = infoTmp.Customize8?.Trim(),
					Customize9 = infoTmp.Customize9?.Trim(),
					Customize10 = infoTmp.Customize10?.Trim(),
					InvoiceAddress = infoTmp.Address?.Trim(),
					InvoiceReceive = infoTmp.FullName?.Trim(),
					InvoiceTel = infoTmp.Mobile?.Trim(),
					InvoiceMail = infoTmp.Mail?.Trim(),
					PaymentTypeId = payType,
					HasVisa = false,
					HasHotel = false,
					HasMeal = false,
					HasPage = false,
					HasContributed = false,
					HasPaid = false,
					HasArrived = false,
					HasPrinted = false,
					HasDinner = false,
					HasInvoiced = false,
					HasSendMail = false,
					HasSendPaid = false,
					IsRefund = false,
					TransferEvidenceId = infoTmp.ConferenceId.ToString() + ConstValues.strHyphen + LoginUser.UserID,
					TotalFee = infoTmp.TotalFee,
					Discount = infoTmp.Discount,
					Currency = infoTmp.Currency?.Trim(),
					Sort = ConstValues.DefaultSort,
					Creator = LoginUser.UserName,
					CreateIP = LoginUser.IP,
					CreateDate = DateTime.Now
				};
				#endregion

				#region QRCode&BarCode
				var conferenceInfo = await DbContext.ConferenceMaster.AsNoTracking()
						.FirstOrDefaultAsync(x => x.ID == result.ConferenceId);

				result.QRCode = Utility.CreateQRCode(code,
						Server.MapPath(Path.Combine(ConstValues.QRCodeImg, conferenceInfo.ConferenceNickName)),
						Path.Combine(ConstValues.QRCodeImg, conferenceInfo.ConferenceNickName));

				result.BarCode = Utility.CreateBarCode(code,
						Server.MapPath(Path.Combine(ConstValues.BarCodeImg, conferenceInfo.ConferenceNickName)),
						Path.Combine(ConstValues.BarCodeImg, conferenceInfo.ConferenceNickName));
				#endregion

				#region VisaInfo
				if (!string.IsNullOrEmpty(model.VisaId))
				{
					var visaInfo = new VisaInfo
					{
						ID = Utility.GetTableId(ConstValues.TableName.VisaInfo),
						ConferenceId = model.ConferenceId,
						AttendenceId = code,
						FirstName = model.FirstName?.Trim(),
						FamilyName = model.FamilyName?.Trim(),
						FullName = Utility.SetFullName(model.FamilyName?.Trim(), model.FirstName?.Trim(),
								Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En ?
									ConstValues.Language.En : ConstValues.Language.Cn),
						Title = model.Title,
						Affiliation = model.Affiliation?.Trim(),
						Address = model.Address?.Trim(),
						CityStr = model.City,
						StateStr = model.State,
						ZipCode = model.ZipCode?.Trim(),
						Country = model.Country,
						Mail = model.Mail?.Trim(),
						Telephone = model.Telephone?.Trim(),
						Birthdate = model.Birthdate,
						Gender = model.Gender == "Female" ? false : true,
						PassportNum = model.PassportNum?.Trim(),
						IssueOfCountry = model.CountryOfIssue?.Trim(),
						DateOfIssue = model.DateOfIssue,
						ExpiryDate = model.ExpiryDate,
						ArrivalDate = model.ArrivalDate,
						DeparturalDate = model.DeparturalDate,
						Remark = model.Remark,
						Status = ConstValues.InvitationStatus.已申请,
						SendStatus = ConstValues.SendStatus.未发送,
						Creator = LoginUser.UserName,
						CreateDate = DateTime.Now,
						CreateIP = LoginUser.IP,
					};

					DbContext.VisaInfo.Add(visaInfo);

					result.HasVisa = true;
				}
				#endregion

				#region ProfilePaymentInfo
				double totalFee = 0;
				double totalDiscount = 0;
				var currency = string.Empty;

				if (!string.IsNullOrEmpty(infoTmp.PageFeeId))
				{
					var feeMaster = DbContext.FeePageMaster.AsNoTracking()
							.FirstOrDefault(x => x.ID == infoTmp.PageFeeId);
					if (feeMaster != null && feeMaster.Price.HasValue)
					{
						var profilePaymentInfo = new ProfilePaymentInfo
						{
							ID = Utility.GetTableId(ConstValues.TableName.ProfilePaymentInfo),
							UserName = LoginUser.UserName,
							AttendenceId = code,
							FeeType = ConstValues.FeeType.版面费,
							FeeId = infoTmp.PageFeeId,
							Count = 1,
							Fee = feeMaster.Price.Value,
							Discount = feeMaster.Price.Value,
							Currency = feeMaster.Currency,
							Creator = LoginUser.UserName,
							CreateIP = LoginUser.IP,
							CreateDate = DateTime.Now
						};

						DbContext.ProfilePaymentInfo.Add(profilePaymentInfo);

						totalFee += feeMaster.Price.Value;
						totalDiscount += feeMaster.Price.Value;
						currency = feeMaster.Currency;
						result.HasPage = true;
					}
				}

				if (!string.IsNullOrEmpty(infoTmp.AttendenceFeeId))
				{
					var feeMaster = DbContext.FeeAttendenceMaster.AsNoTracking()
							.FirstOrDefault(x => x.ID == infoTmp.AttendenceFeeId);
					if (feeMaster != null && feeMaster.Price.HasValue)
					{
						double originFee = feeMaster.Price.Value;
						double discountFee = feeMaster.Discount.HasValue ? feeMaster.Discount.Value : feeMaster.Price.Value;

						var profilePaymentInfo = new ProfilePaymentInfo
						{
							ID = Utility.GetTableId(ConstValues.TableName.ProfilePaymentInfo),
							UserName = LoginUser.UserName,
							AttendenceId = code,
							FeeType = ConstValues.FeeType.参会费,
							FeeId = infoTmp.AttendenceFeeId,
							Count = 1,
							Fee = originFee,
							Discount = discountFee,
							Currency = feeMaster.Currency,
							Creator = LoginUser.UserName,
							CreateIP = LoginUser.IP,
							CreateDate = DateTime.Now
						};

						DbContext.ProfilePaymentInfo.Add(profilePaymentInfo);

						totalFee += originFee;
						totalDiscount += discountFee;
						currency = feeMaster.Currency;
					}
				}

				if (!string.IsNullOrEmpty(infoTmp.MealFeeId))
				{
					var feeMaster = DbContext.FeeMealMaster.AsNoTracking()
							.FirstOrDefault(x => x.ID == infoTmp.MealFeeId);
					if (feeMaster != null && feeMaster.Price.HasValue)
					{
						var profilePaymentInfo = new ProfilePaymentInfo
						{
							ID = Utility.GetTableId(ConstValues.TableName.ProfilePaymentInfo),
							UserName = LoginUser.UserName,
							AttendenceId = code,
							FeeType = ConstValues.FeeType.餐费,
							FeeId = infoTmp.MealFeeId,
							Count = 1,
							Fee = feeMaster.Price.Value,
							Discount = feeMaster.Price.Value,
							Currency = feeMaster.Currency,
							Creator = LoginUser.UserName,
							CreateIP = LoginUser.IP,
							CreateDate = DateTime.Now
						};

						DbContext.ProfilePaymentInfo.Add(profilePaymentInfo);

						totalFee += feeMaster.Price.Value;
						totalDiscount += feeMaster.Price.Value;
						currency = feeMaster.Currency;
						result.HasMeal = true;
					}
				}

				if (!string.IsNullOrEmpty(infoTmp.OtherFeeList) && !string.IsNullOrEmpty(infoTmp.OtherFeeCount))
				{
					var otherFeeList = infoTmp.OtherFeeList.Split(ConstValues.chrSemicolon);
					var otherFeeCountList = infoTmp.OtherFeeCount.Split(ConstValues.chrSemicolon);
					for (var index = 0; index < otherFeeList.Length; index++)
					{
						string tmp = otherFeeList[index];
						int count = Convert.ToInt32(otherFeeCountList[index]);

						var feeMaster = DbContext.FeeOtherMaster.AsNoTracking()
								.FirstOrDefault(x => x.ID == tmp);

						if (feeMaster != null && count != 0)
						{
							var profilePaymentInfo = new ProfilePaymentInfo
							{
								ID = Utility.GetTableId(ConstValues.TableName.ProfilePaymentInfo),
								UserName = LoginUser.UserName,
								AttendenceId = code,
								FeeType = ConstValues.FeeType.其他费用,
								FeeId = tmp,
								Count = count,
								Fee = feeMaster.Price.Value * count,
								Discount = feeMaster.Price.Value * count,
								Currency = feeMaster.Currency,
								Creator = LoginUser.UserName,
								CreateIP = LoginUser.IP,
								CreateDate = DateTime.Now
							};

							DbContext.ProfilePaymentInfo.Add(profilePaymentInfo);

							totalFee += feeMaster.Price.Value * count;
							totalDiscount += feeMaster.Price.Value * count;
							currency = feeMaster.Currency;
						}
					}
				}
				//同期活动费用
				if (!string.IsNullOrEmpty(infoTmp.ConcurrentEventList) && !string.IsNullOrEmpty(infoTmp.ConcurrentEventCount))
				{
					var concurrentEventList = infoTmp.ConcurrentEventList.Split(ConstValues.chrComma);
					var concurrentEventCount = infoTmp.ConcurrentEventCount.Split(ConstValues.chrComma);
					for (var index = 0; index < concurrentEventList.Length; index++)
					{
						string tmp = concurrentEventList[index];
						int count = int.Parse(concurrentEventCount[index]);

						var feeMaster = DbContext.FeeConcurrentEventMaster.AsNoTracking().FirstOrDefault(x => x.ConcurrentEventId == tmp && x.Status == ConstValues.Status.正常
									&& (x.AttendenceIdentityId == infoTmp.AttendenceIdentityId || x.AttendenceIdentityId == ConstValues.ApplicableForAll)
									&& x.Currency == infoTmp.Currency);

						if (feeMaster != null && count != 0)
						{
							var profilePaymentInfo = new ProfilePaymentInfo
							{
								ID = Utility.GetTableId(ConstValues.TableName.ProfilePaymentInfo),
								UserName = LoginUser.UserName,
								AttendenceId = code,
								FeeType = ConstValues.FeeType.同期活动费,
								FeeId = feeMaster.ID,
								Count = count,
								Fee = feeMaster.Price.Value * count,
								Discount = feeMaster.Price.Value * count,
								Currency = feeMaster.Currency,
								Creator = LoginUser.UserName,
								CreateIP = LoginUser.IP,
								CreateDate = DateTime.Now
							};

							DbContext.ProfilePaymentInfo.Add(profilePaymentInfo);

							totalFee += feeMaster.Price.Value * count;
							totalDiscount += feeMaster.Price.Value * count;
							currency = feeMaster.Currency;
						}
					}
				}

				if (totalFee != 0)
				{
					result.TotalFee = totalFee;
				}
				if (totalDiscount != 0)
				{
					result.Discount = totalDiscount;
				}
				if (!string.IsNullOrEmpty(currency))
				{
					result.Currency = currency;
				}
				#endregion

				DbContext.ConferenceAttendenceInfo.Add(result);
				DbContext.ConferenceAttendenceInfoTmp.Remove(infoTmp);
				
				#region SysUserMaster
				var sysUserMaster = await DbContext.SysUserMaster.FirstOrDefaultAsync(x => x.UserName == LoginUser.UserName);
				if (sysUserMaster.HasUpdate == false && sysUserMaster.Language == ConstValues.Language.Cn && !conferenceInfo.IsInternational)
				{
					sysUserMaster.FullName = !string.IsNullOrEmpty(infoTmp.FullName) ? infoTmp.FullName?.Trim() :
								Utility.SetFullName(infoTmp.FamilyName?.Trim(), infoTmp.FirstName?.Trim(), ConstValues.Language.Cn);
					sysUserMaster.FamilyName = infoTmp.FamilyName;
					sysUserMaster.FirstName = infoTmp.FirstName;
					sysUserMaster.Gender = infoTmp.Gender;
					sysUserMaster.Degree = infoTmp.Degree;
					sysUserMaster.Affiliation = infoTmp.Affiliation;
					sysUserMaster.Department = infoTmp.Department;
					sysUserMaster.Address = infoTmp.Address;
					sysUserMaster.Country = infoTmp.Country;
					sysUserMaster.StateStr = infoTmp.StateStr;
					sysUserMaster.CityStr = infoTmp.CityStr;
					sysUserMaster.Telephone = infoTmp.Telephone;
					sysUserMaster.Mobile = infoTmp.Mobile;
					sysUserMaster.HasUpdate = true;
					sysUserMaster.Updater = LoginUser.UserName;
					sysUserMaster.UpdateDate = DateTime.Now;
					sysUserMaster.UpdateIP = LoginUser.IP;
				}
				#endregion

				#region SysUserMasterEn
				if (sysUserMaster.Language == ConstValues.Language.Cn && conferenceInfo.IsInternational)
				{
					var sysUserMasterEn = await DbContext.SysUserMasterEn.FirstOrDefaultAsync(x => x.UserName == LoginUser.UserName);
					if (sysUserMasterEn != null && sysUserMasterEn.HasUpdate == false)
					{
						sysUserMasterEn.FullName = !string.IsNullOrEmpty(infoTmp.FullName) ? infoTmp.FullName?.Trim() :
									Utility.SetFullName(infoTmp.FamilyName?.Trim(), infoTmp.FirstName?.Trim(), ConstValues.Language.En);
						sysUserMasterEn.FamilyName = infoTmp.FamilyName;
						sysUserMasterEn.FirstName = infoTmp.FirstName;
						sysUserMasterEn.Gender = infoTmp.Gender;
						sysUserMasterEn.Degree = infoTmp.Degree;
						sysUserMasterEn.Affiliation = infoTmp.Affiliation;
						sysUserMasterEn.Department = infoTmp.Department;
						sysUserMasterEn.Address = infoTmp.Address;
						sysUserMasterEn.Country = infoTmp.Country;
						sysUserMasterEn.Telephone = infoTmp.Telephone;
						sysUserMasterEn.Mobile = infoTmp.Mobile;
						sysUserMasterEn.HasUpdate = true;
						sysUserMasterEn.Updater = LoginUser.UserName;
						sysUserMasterEn.UpdateDate = DateTime.Now;
						sysUserMasterEn.UpdateIP = LoginUser.IP;
					}
					else
					{
						sysUserMasterEn = new SysUserMasterEn();
						sysUserMasterEn.ID = Utility.GetTableId(ConstValues.TableName.SysUserMasterEn);
						sysUserMasterEn.UserName = LoginUser.UserName;
						sysUserMasterEn.Mail = LoginUser.Email;
						sysUserMasterEn.Call = sysUserMaster.Call;
						sysUserMasterEn.Career = sysUserMaster.Career;
						sysUserMasterEn.OcisCode = sysUserMaster.OcisCode;
						sysUserMasterEn.HasUpdate = true;
						sysUserMasterEn.Status = ConstValues.Status.正常;
						sysUserMasterEn.Sort = ConstValues.DefaultSort;
						sysUserMasterEn.Creator = LoginUser.UserName;
						sysUserMasterEn.CreateDate = DateTime.Now;
						sysUserMasterEn.CreateIP = LoginUser.IP;

						sysUserMasterEn.FullName = !string.IsNullOrEmpty(infoTmp.FullName) ? infoTmp.FullName?.Trim() :
									Utility.SetFullName(infoTmp.FamilyName?.Trim(), infoTmp.FirstName?.Trim(), ConstValues.Language.En);
						sysUserMasterEn.FamilyName = infoTmp.FamilyName;
						sysUserMasterEn.FirstName = infoTmp.FirstName;
						sysUserMasterEn.Gender = infoTmp.Gender;
						sysUserMasterEn.Degree = infoTmp.Degree;
						sysUserMasterEn.Affiliation = infoTmp.Affiliation;
						sysUserMasterEn.Department = infoTmp.Department;
						sysUserMasterEn.Address = infoTmp.Address;
						sysUserMasterEn.Country = infoTmp.Country;
						sysUserMasterEn.Telephone = infoTmp.Telephone;
						sysUserMasterEn.Mobile = infoTmp.Mobile;

						DbContext.SysUserMasterEn.Add(sysUserMasterEn);
					}
				}
				#endregion

				#region InvoiceMaster
				if (isSelf == ConstValues.GenericSelectionType.有)
				{
					if (sysUserMaster != null)
					{
						sysUserMaster.InvoiceTitle = infoTmp.InvoiceTitle;
						sysUserMaster.TaxNum = infoTmp.TaxNum;
						sysUserMaster.AddressAndTel = infoTmp.AddressAndTel;
						sysUserMaster.Bank = infoTmp.Bank;
						sysUserMaster.BankAccount = infoTmp.BankAccount;
					}
				}

				Utility.SaveInvoiceInfo(LoginUser, new InvoiceModel
				{
					InvoiceTitle = infoTmp.InvoiceTitle,
					TaxNum = infoTmp.TaxNum,
					AddressAndTel = infoTmp.AddressAndTel,
					Bank = infoTmp.Bank,
					BankAccount = infoTmp.BankAccount,
				});
				#endregion

				await DbContext.SaveChangesAsync();

				Session.Remove("TmpCode");

				#region SendMail
				var conferenceMailServerMaster = await DbContext.ConferenceMailServerMaster.AsNoTracking()
						.FirstOrDefaultAsync(x => x.Status == ConstValues.Status.正常 &&
							x.MailType == ConstValues.MailType.注册服务邮箱 &&
							x.ConferenceId == model.ConferenceId);

				if (conferenceMailServerMaster == null)
				{
					conferenceMailServerMaster = await DbContext.ConferenceMailServerMaster.AsNoTracking()
							.FirstOrDefaultAsync(x => x.Status == ConstValues.Status.正常 &&
								x.MailType == ConstValues.MailType.会议邮箱 &&
								x.ConferenceId == model.ConferenceId);
				}

				if (conferenceMailServerMaster != null)
				{
					var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
							.FirstOrDefaultAsync(x => x.ID == infoTmp.ConferenceId);

					MailSender sender = new MailSender(conferenceMailServerMaster.MailServer);
					MailAddress from = new MailAddress(conferenceMailServerMaster.Mail);
					MailAddress to = new MailAddress(result.Mail);

					MailMessage message = Utility.GetMailContent(new MailModel
					{
						MailRemark = ConstValues.MailSubject.会议注册,
						Language = conferenceMaster.IsInternational ? ConstValues.Language.En : ConstValues.Language.Cn,
						UserName = result.FullName,
						ConferenceId = conferenceMaster.ID,
						ConferenceName = conferenceMaster.ConferenceName,
						ConferenceNickName = conferenceMaster.ConferenceNickName,
						ConferenceUrl = !string.IsNullOrEmpty(conferenceMaster.HomePageUrl) ? conferenceMaster.HomePageUrl :
							ConstValues.SystemWeb + "/MeetingMain/Index/" + conferenceMaster.ConferenceNickName,
						AttendenceId = code
					});
					message.To.Add(to);
					//message.Attachments.Add(new Attachment(Server.MapPath(result.QRCode)));

					if (!string.IsNullOrWhiteSpace(conferenceMailServerMaster.CcMail))
					{
						foreach (string cc in conferenceMailServerMaster.CcMail.Split(ConstValues.chrSemicolon))
						{
							if (!string.IsNullOrWhiteSpace(cc) && cc.Contains(ConstValues.chrAt))
							{
								message.Bcc.Add(new MailAddress(cc.Trim()));
							}
						}
					}

					var mailTask = Task.Run(() =>
					{
						sender.SendMail(from, message, conferenceMailServerMaster.Password);
					});
				}
				#endregion

				transaction.Commit();
			}

			return JsonCamelCase(Utility.CreateSuccessJsonResult(Resources.SuccessAttend, code),
					JsonRequestBehavior.AllowGet);
		}

		[Route("GetPageItem/{conferenceId}")]
		public async Task<JsonCamelCaseResult> GetPageItem(string conferenceId)
		{
			var result = await DbContext.ConferenceAttendenceForm.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 &&
						x.ConferenceId == conferenceId)
					.OrderBy(x => x.DisplayPage)
					.ThenBy(x => x.Sort)
					.ThenBy(x => x.DisplayName)
					.ToListAsync();

			return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty, result),
					JsonRequestBehavior.AllowGet);
		}

		[Route("ValidateCode")]
		public ActionResult ValidateCode()
		{
			ValidatedCode v = new ValidatedCode();
			string code = v.CreateVerifyCode();
			Bitmap image = v.CreateImageCode(code);
			Session["RecCheckCode"] = code;
			using (MemoryStream stream = new MemoryStream())
			{
				image.Save(stream, ImageFormat.Jpeg);
				byte[] data = new byte[stream.Length];
				stream.Seek(0, SeekOrigin.Begin);
				stream.Read(data, 0, Convert.ToInt32(stream.Length));
				Response.ContentType = "image/jpeg";
				Response.AddHeader("Content-Type", "image/jpeg");
				Response.AddHeader("Content-Disposition", "inline");

				Response.BinaryWrite(data);
				Response.Flush();
				Response.End();
				return new EmptyResult();
			}
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		[ValidateInput(false)]
		public async Task<JsonResult> RecToOthers(ConferenceAttendenceInfoModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId);

			if (Session["RecCheckCode"] == null || Session["RecCheckCode"].ToString().ToLower() != model.ValidateCode.ToLower())
			{
				//ModelState.AddModelError("", Resources.MsgCheckCodeError);
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgCheckCodeError), JsonRequestBehavior.AllowGet);
			}
			else
			{
				var profileRecommendRecordId = Utility.GetTableId(ConstValues.TableName.ProfileRecommendRecord);
				var profileRecommendRecord = new ProfileRecommendRecord
				{
					ID = profileRecommendRecordId,
					ConferenceId = model.ConferenceId,
					UserId = LoginUser.UserID,
					FamilyName = model.recFamilyName,
					FirstName = model.recFirstName,
					Mail = model.recMail,
					Recommendation = model.recRecommendContent,
					Creator = LoginUser.UserName,
					CreateIP = LoginUser.IP,
					CreateDate = DateTime.Now
				};
				DbContext.ProfileRecommendRecord.Add(profileRecommendRecord);

				#region SendMail
				var conferenceMailServerMaster = await DbContext.ConferenceMailServerMaster.AsNoTracking()
						.FirstOrDefaultAsync(x => x.Status == ConstValues.Status.正常 &&
							x.MailType == ConstValues.MailType.会议邮箱 &&
							x.ConferenceId == model.ConferenceId);

				if (conferenceMailServerMaster != null)
				{
					var user = await DbContext.SysUserMaster.AsNoTracking()
							.FirstOrDefaultAsync(x => x.ID == LoginUser.UserID);

					var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
							.FirstOrDefaultAsync(x => x.ID == model.ConferenceId);

					MailSender sender = new MailSender(conferenceMailServerMaster.MailServer);
					MailAddress from = new MailAddress(conferenceMailServerMaster.Mail);
					MailAddress to = new MailAddress(model.recMail);

					var time = DateTime.Now.ToString("yyyy-MM-dd");
					MailMessage message = Utility.GetMailContent(new MailModel
					{
						MailRemark = ConstValues.MailSubject.会议推荐,
						Language = conferenceMaster.IsInternational ? ConstValues.Language.En : ConstValues.Language.Cn,
						UserName = user.FullName,
						ConferenceId = conferenceMaster.ID,
						ConferenceName = conferenceMaster.ConferenceName,
						ConferenceUrl = !string.IsNullOrEmpty(conferenceMaster.HomePageUrl) ? conferenceMaster.HomePageUrl :
							ConstValues.SystemWeb + "/MeetingMain/Index/" + conferenceMaster.ConferenceNickName,
						Recommedation = model.recRecommendContent
					});
					message.To.Add(to);

					if (!string.IsNullOrWhiteSpace(conferenceMailServerMaster.CcMail))
					{
						foreach (string cc in conferenceMailServerMaster.CcMail.Split(ConstValues.chrSemicolon))
						{
							if (!string.IsNullOrWhiteSpace(cc) && cc.Contains(ConstValues.chrAt))
							{
								message.Bcc.Add(new MailAddress(cc.Trim()));
							}
						}
					}

					var mailTask = Task.Run(() =>
					{
						sender.SendMail(from, message, conferenceMailServerMaster.Password);
					});
				}
				#endregion

				await DbContext.SaveChangesAsync();

				return JsonCamelCase(Utility.CreateSuccessJsonResult(Resources.SuccessSendMail), JsonRequestBehavior.AllowGet);
			}
		}

		public ActionResult RegisterClose()
		{
			return View(this.GetLoginModel());
		}

		public LoginViewModel GetLoginModel()
		{
			string userName = Session["userName"]?.ToString();
			string password = Session["password"]?.ToString();
			string validateCode = Session["ValidateCode"]?.ToString();

			var model = new LoginViewModel();

			model.AccountName = userName;
			model.Password = password;
			model.ValidateCode = validateCode;

			return model;
		}

		[Route("GetPaymentInfoData/{conferenceId}")]
		public async Task<JsonCamelCaseResult> GetPaymentInfoData(string conferenceId)
		{
			var result = new List<Dictionary<string, string>>();
			var code = Session["TmpCode"]?.ToString();
			var tmp = await DbContext.ConferenceAttendenceInfoTmp.FirstOrDefaultAsync(x => x.ID == code);
			var conferenceMaster = DbContext.ConferenceMaster.FirstOrDefault(x => x.ID == conferenceId);
			var number = 1;
			double? totalPrice = 0;

			if (tmp.AttendenceFeeId != null)
			{
				var feeAttendenceMaster = DbContext.FeeAttendenceMaster.FirstOrDefault(x => x.ID == tmp.AttendenceFeeId && x.Price != 0);
				if (feeAttendenceMaster != null)
				{
					Dictionary<string, string> paymentInfo = new Dictionary<string, string>();
					var displayName = DbContext.ConferenceAttendenceForm.AsNoTracking().FirstOrDefault(y => y.ConferenceId == conferenceId && y.Key == "Attendence")?.DisplayName;
					var feeName = feeAttendenceMaster.RegisterType;
					var currency = feeAttendenceMaster.Currency;
					var name = displayName + ConstValues.chrSpace + ConstValues.chrComma + ConstValues.chrSpace + feeName;
					var fee = conferenceMaster.BenefitStartDate < DateTime.Now && DateTime.Now < conferenceMaster.BenefitEndDate ? feeAttendenceMaster.Discount : feeAttendenceMaster.Price;
					paymentInfo.Add("number", number.ToString());
					paymentInfo.Add("currency", currency);
					paymentInfo.Add("name", name);
					paymentInfo.Add("fee", fee.Value.ToString("N2"));
					number += 1;
					result.Add(paymentInfo);
					totalPrice += fee;
					paymentInfo.Add("totalPrice", totalPrice.Value.ToString("N2"));
				}
			}

			if (tmp.PageFeeId != null)
			{
				var feePageMaster = DbContext.FeePageMaster.FirstOrDefault(x => x.ID == tmp.PageFeeId && x.Price != 0);
				if (feePageMaster != null)
				{
					Dictionary<string, string> paymentInfo = new Dictionary<string, string>();
					var displayName = DbContext.ConferenceAttendenceForm.AsNoTracking().FirstOrDefault(y => y.ConferenceId == conferenceId && y.Key == "Page")?.DisplayName;
					var feeName = feePageMaster.FeeName;
					var currency = feePageMaster.Currency;
					var name = displayName + ConstValues.chrSpace + ConstValues.chrComma + ConstValues.chrSpace + feeName;
					var fee = feePageMaster.Price;
					paymentInfo.Add("number", number.ToString());
					paymentInfo.Add("currency", currency);
					paymentInfo.Add("name", name);
					paymentInfo.Add("fee", fee.Value.ToString("N2"));
					number += 1;
					result.Add(paymentInfo);
					totalPrice += fee;
					paymentInfo.Add("totalPrice", totalPrice.Value.ToString("N2"));
				}
			}

			if (tmp.MealFeeId != null)
			{
				var feeMealMaster = DbContext.FeeMealMaster.FirstOrDefault(x => x.ID == tmp.MealFeeId && x.Price != 0);
				if (feeMealMaster != null)
				{
					Dictionary<string, string> paymentInfo = new Dictionary<string, string>();
					var displayName = DbContext.ConferenceAttendenceForm.AsNoTracking().FirstOrDefault(y => y.ConferenceId == conferenceId && y.Key == "Meal")?.DisplayName;
					var feeName = feeMealMaster.FeeName;
					var currency = feeMealMaster.Currency;
					var name = displayName + ConstValues.chrSpace + ConstValues.chrComma + ConstValues.chrSpace + feeName;
					var fee = feeMealMaster.Price;
					paymentInfo.Add("number", number.ToString());
					paymentInfo.Add("currency", currency);
					paymentInfo.Add("name", name);
					paymentInfo.Add("fee", fee.Value.ToString("N2"));
					number += 1;
					result.Add(paymentInfo);
					totalPrice += fee;
					paymentInfo.Add("totalPrice", totalPrice.Value.ToString("N2"));
				}
			}

			if (tmp.OtherFeeList != null)
			{
				var otherFeeList = tmp.OtherFeeList.Split(ConstValues.chrSemicolon);
				var otherFeeCount = tmp.OtherFeeCount.Split(ConstValues.chrSemicolon);
				for (var i = 0; i < otherFeeList.Length; i++)
				{
					var id = otherFeeList[i];
					var feeOtherMaster = DbContext.FeeOtherMaster.FirstOrDefault(x => x.ID == id && x.Price != 0);
					if (feeOtherMaster != null)
					{
						Dictionary<string, string> paymentInfo = new Dictionary<string, string>();
						var displayName = DbContext.ConferenceAttendenceForm.AsNoTracking().FirstOrDefault(y => y.ConferenceId == conferenceId && y.Key == "Other")?.DisplayName;
						var feeName = feeOtherMaster.FeeName;
						var currency = feeOtherMaster.Currency;
						var name = displayName + ConstValues.chrSpace + ConstValues.chrComma + ConstValues.chrSpace + feeName;
						var fee = feeOtherMaster.Price * int.Parse(otherFeeCount[i]);
						paymentInfo.Add("number", number.ToString());
						paymentInfo.Add("currency", currency);
						paymentInfo.Add("name", name);
						paymentInfo.Add("fee", fee.Value.ToString("N2"));
						number += 1;
						result.Add(paymentInfo);
						totalPrice += fee;
						paymentInfo.Add("totalPrice", totalPrice.Value.ToString("N2"));
					}
				}
			}

			if (tmp.ConcurrentEventCount != null)
			{
				var concurrentEventCount = tmp.ConcurrentEventCount.Split(ConstValues.chrComma);
				var concurrentEventList = tmp.ConcurrentEventList.Split(ConstValues.chrComma);
				for (var i = 0; i < concurrentEventList.Length; i++)
				{
					var id = concurrentEventList[i];
					var concurrentEventMaster = DbContext.ConcurrentEventMaster.FirstOrDefault(x => x.ID == id);
					if (concurrentEventMaster != null)
					{
						Dictionary<string, string> paymentInfo = new Dictionary<string, string>();
						var feeConcurrentEventMaster = DbContext.FeeConcurrentEventMaster.FirstOrDefault(x => x.ConcurrentEventId == concurrentEventMaster.ID
												&& x.Status == ConstValues.Status.正常
												&& (x.AttendenceIdentityId == tmp.AttendenceIdentityId || x.AttendenceIdentityId == ConstValues.ApplicableForAll)
												&& x.Price != 0
												&& x.Currency == tmp.Currency);
						if (feeConcurrentEventMaster != null)
						{
							var displayName = DbContext.ConferenceAttendenceForm.AsNoTracking().FirstOrDefault(y => y.ConferenceId == conferenceId && y.Key == "ConcurrentEvent")?.DisplayName;
							var feeName = concurrentEventMaster.EventName;
							var currency = feeConcurrentEventMaster.Currency;
							var name = displayName + ConstValues.chrSpace + ConstValues.chrComma + ConstValues.chrSpace + feeName;
							var fee = feeConcurrentEventMaster.Price * int.Parse(concurrentEventCount[i]);
							paymentInfo.Add("number", number.ToString());
							paymentInfo.Add("currency", currency);
							paymentInfo.Add("name", name);
							paymentInfo.Add("fee", fee.Value.ToString("N2"));
							number += 1;
							result.Add(paymentInfo);
							totalPrice += fee;
							paymentInfo.Add("totalPrice", totalPrice.Value.ToString("N2"));
						}
					}
				}
			}

			return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty, result),
					JsonRequestBehavior.AllowGet);
		}
	}
}